import Axios from 'axios';
import { Message, Loading } from 'element-ui';

Axios.defaults.baseURL = 'http://192.168.0.113:9999'

let loading
// this.$nextTick(() => { // 以服务的方式调用的 Loading 需要异步关闭
//   loadingInstance.close();
// });

/* axios 拦截器 */
// http请求拦截器
Axios.interceptors.request.use(
  config => {
    loading = Loading.service({
      lock: true,
      text: 'Loading',
      spinner: 'el-icon-loading',
      background: 'rgba(0, 0, 0, 0.7)'
    });
    return config
  },
  err => {
    return Promise.reject(err)
  }
)
// http响应拦截器
Axios.interceptors.response.use(
  res => {
    loading.close()
    // 响应成功关闭loadin
    if (res.data.success) {
      this.$message({
        message: res.data.msg,
        type: "success"
      });
      return res
    } else {
      this.$message({
        message: res.data.msg,
        type: "error"
      });
      return Promise.reject(res)
    }

  },
  err => {
    loading.close()
    Message({
      type: 'error',
      message: '服务器出错，请重试'
    })
    return Promise.reject(err)
  }
)